package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.dao.StudentDao;
import org.example.domain.Student;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author taoshen
 * @create 2021/4/1
 */
public class MyApp {
    public static void main(String[] args) throws IOException {
        //拿到主配置文件在target/classess下的路径
        String mybatis_xml = "mybatis.xml";
        InputStream is = Resources.getResourceAsStream(mybatis_xml);

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(is);
        SqlSession sqlSession = factory.openSession();

        /**
         * 两种方式都是可以的 第一种通过拼接dao接口和函数名 通过SqlSession提供的api来完成
         * 第二种是SqlSession通过getMapper拿到dao接口 来完成对接口的在sql上的增强
         */
        String sqlId = "org.example.dao.StudentDao" + "." + "selectStudents";
        List<Object> students = sqlSession.selectList(sqlId);
        students.forEach(System.out::println);

        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        List<Student> students1 = mapper.selectStudents();
        students1.forEach(System.out::println);
        sqlSession.close();
    }
}
